//
//  LIUShareViewController.m
//  EasyNotes
//
//  Created by 劉裕 on 3/6/2016.
//  Copyright © 2016年 劉裕. All rights reserved.
//

#import "LIUShareViewController.h"
#import "GCDWebUploader.h"
#import "NSString+Category.h"
#import <Masonry/Masonry.h>
#import "LIUQRCode.h"

@interface LIUShareViewController () <GCDWebUploaderDelegate>

@property (nonatomic, strong) GCDWebUploader *webServer;
@property (nonatomic, weak) UILabel *isStartMsg;
@property (nonatomic, weak) UIImageView *qrCode;
@property (nonatomic, weak) UIView *backView;
@end

@implementation LIUShareViewController

#pragma mark - life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Lazy

- (GCDWebUploader *)webServer {
    if (nil == _webServer) {
        _webServer = [[GCDWebUploader alloc] initWithUploadDirectory:[NSString.document stringByAppendingPathComponent:@"HTML"]];
        _webServer.delegate = self;
        _webServer.allowHiddenItems = NO;
    }
    return _webServer;
}

#pragma mark - private methods

- (void)setupView {
    WS(ws)
    self.title = NSLocalizedString(@"leftItem_Share", nil);
    UILabel *isStartMsg = [UILabel new];
    isStartMsg.numberOfLines = 0;
    isStartMsg.text = NSLocalizedString(@"openserver", nil);
    [self.view addSubview:isStartMsg];
    self.isStartMsg = isStartMsg;
    
    UISwitch *switchBtn = [UISwitch new];
    [self.view addSubview:switchBtn];
    [switchBtn addTarget:self action:@selector(switchBtnAction:) forControlEvents:UIControlEventValueChanged];
    
    UIView *backView = [UIView new];
    backView.backgroundColor = LIUColor(200, 200, 200, 1.0f);
    backView.layer.masksToBounds = YES;
    backView.layer.cornerRadius = 5.0f;
    [self.view addSubview:backView];
    self.backView = backView;
    
    
    UIImageView *qrCode = [UIImageView new];
    [backView addSubview:qrCode];
    self.qrCode = qrCode;
    
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(ws.view).offset(20);
        make.centerX.equalTo(ws.view);
        make.size.mas_equalTo(CGSizeMake(200, 200));
    }];
    
    [qrCode mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(backView).insets(UIEdgeInsetsMake(10, 10, 10, 10));
    }];
    
    
    [switchBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backView);
        make.top.equalTo(backView).offset(-35);
    }];
    
    [isStartMsg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(backView).offset(30);
        make.left.equalTo(backView);
    }];
    
    backView.hidden = YES;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"menu"] style:UIBarButtonItemStylePlain target:self action:@selector(menuBtnClick)];
}

- (void)menuBtnClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)switchBtnAction:(UISwitch *)sender {
    BOOL isBtnOn = sender.isOn;
    if (isBtnOn) {
        if ([self.webServer start]) {
            self.isStartMsg.text = [NSString stringWithFormat:NSLocalizedString(@"%@", nil), [self.webServer.serverURL description]];
            self.qrCode.image = [LIUQRCode createQRCodeWithStr:self.isStartMsg.text];
            self.backView.hidden = NO;
        }
    } else {
        self.isStartMsg.text = NSLocalizedString(@"shareServer", nil);
        self.backView.hidden = YES;
        [self.webServer stop];
        self.webServer = nil;
    }
}
@end
